package com.tigase.controller;

import com.tigase.bean.TigaseMessage;
import com.tigase.service.MessageService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qt2 on 2017/7/21.
 */

@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 保存消息记录
     * @param tigaseMessage
     * @return
     */
    @RequestMapping(value = "/save")
    public Map<String,Object> insertMessage(@ModelAttribute TigaseMessage tigaseMessage){
        Map<String,Object> map = new HashMap<>();
        int message = messageService.insertMessage(tigaseMessage);
        if(message > 0) {
            map.put("msg","保存成功");
            map.put("flag",true);
        }else {
            map.put("msg","保存失败");
            map.put("flag",false);
        }
        return map;
    }

    /**
     * 获取消息
     * @param messageFrom
     * @param messageTo
     * @return
     */
    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public Map<String,Object> getAll(@RequestParam(value = "messageFrom",required = false) String messageFrom,
                                     @RequestParam(value = "messageTo",required = false)  String messageTo){
        Map<String,Object> map = new HashMap<>();
        List<TigaseMessage> list = messageService.getAll(messageFrom,messageTo);
        if(list.size()>0){
            map.put("list",list);
        }else {
            map.put("list",null);
        }
        map.put("msg","消息获取成功");
        map.put("flag",true);
        return map;
        }

    /**
     * 根据发送时间删除过期消息
     * @param sendTime
     * @return
     */
    @RequestMapping(value = "/delete")
    public Map<String,Object> deleteMessage(@RequestParam(value = "sendTime",required = false) Timestamp sendTime){
        Map<String,Object> map = new HashMap<>();
        Date now = new Date();
        long between = now.getTime()-sendTime.getTime();
        System.out.println(between+"22222222222222");
        //信息有效期为7天
        if(between>(7*24*3600000)){
        messageService.deleteMessage(sendTime);
            map.put("msg","消息删除成功");
            map.put("flag",true);
        }else {
            map.put("msg", "消息删除失败");
            map.put("flag", false);
        }
        return map;
    }

    /**
     * 根据现在时间删除过期消息
     * @param nowTime
     * @return
     */
    @RequestMapping(value = "/move")
    public Map<String,Object> moveMessage(@RequestParam(value = "nowTime",required = false) Timestamp nowTime){
        Map<String,Object> map = new HashMap<>();
        int result = messageService.moveMessage(nowTime);
        System.out.println(result+"555555555555555");
        if(result>0){
            map.put("msg","消息删除成功");
            map.put("flag",true);
        }else {
            map.put("msg", "消息删除失败");
            map.put("flag", false);
        }
        return  map;
    }
}
